package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import model.ConferencePaper;
import model.ProgramChair;
import model.SubProgramChair;

/**
 * This class is a panel that displays a list
 * of assigned manuscripts for the subprogramchair role.
 * 
 * @author Darrion Harris
 *
 */
@SuppressWarnings("serial")
public class SPCAssignedPapersPanel extends Container{

	ProgramChair my_chair = SPCViewport.my_chair;
	SubProgramChair my_subchair = SPCViewport.my_subchair;
	String userName = SPCViewport.my_current_user_name;
	final Dimension row_item_width = new Dimension((SPCViewport.WIDTH - 60)/4, SPCViewport.ROW_HEIGHT);
	
	public SPCAssignedPapersPanel(){
		this.setLayout(new BorderLayout());
		this.setBackground(BaseViewFrame.BACKGROUND_COLOR);
		JPanel pad = new JPanel();
		pad.setOpaque(false);
		pad.add(setUpPaperList());
		
		this.add(pad, BorderLayout.CENTER);
	}
	
	/**
	 * Creates the panel that holds the list of all of the
	 * assigned manuscripts for this subprogram chair
	 * @return the panel with the list of assigned manuscripts.
	 */
	private JPanel setUpPaperList() 
	{
		
	      JPanel paperList = new JPanel();
	      paperList.setOpaque(false);
	      paperList.setLayout(new BoxLayout(paperList, BoxLayout.PAGE_AXIS));
	      ArrayList<JLabel> list_header = new ArrayList<JLabel>();
	      list_header.add(new JLabel("Title"));
	      list_header.add(new JLabel("Author"));
	      list_header.add(new JLabel("Reviewer"));
	      list_header.add(new JLabel("Recommendation"));
	      
	      JPanel header_row = new JPanel();
	      header_row.setBackground(BaseViewFrame.BACKGROUND_COLOR);
	      header_row.setLayout(new FlowLayout());
	      for(JLabel j : list_header)
	      {
	    	  j.setPreferredSize(row_item_width);
	    	  header_row.add(j);
	      }
	      paperList.add(header_row);
	      paperList.add(new JSeparator());
		  for (ConferencePaper paper : my_subchair.getMyPapers())
		  {
			  
			  JPanel row = new JPanel();
			  row.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			  row.setBorder(BorderFactory.createEtchedBorder());
		      row.setLayout(new FlowLayout());
		      
		      JLabel title = new JLabel(paper.getTitle());
		      title.setPreferredSize(row_item_width);
		      JLabel author = new JLabel(paper.getAuthorName());
		      author.setPreferredSize(row_item_width);
		      row.add(title);
		      row.add(author);
			  row.add(createAssignButton(paper));
			  row.add(createRecommendationButton(paper));		  
			  paperList.add(row);
			  paperList.add(Box.createVerticalStrut(10));
			  
		  }
		return paperList;
			
		}
	
	/**
	 * Creates the button for assigning reviewers to a conference paper
	 * @param the_paper that we will be assigning reviewers for.
	 * @return the button responsible for loading the assign a reviewer view.
	 */
	private JButton createAssignButton(final ConferencePaper the_paper)
	{
		JButton button = new JButton("Assign");
		button.setPreferredSize(row_item_width);
		/*
		 * I decided to add action method to each button so I wouldn't have to parse through 
		 * every paper in the ConferencePaper list again just to get the paper associated with this button. 
		 */
		button.addActionListener(new AbstractAction()
	      {  
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 6629756264513590964L;

			@Override
	        public void actionPerformed(final ActionEvent the_event)
	        {
	        	SPCViewport.loadReviewersPanel(the_paper);
	        }
	      });
		return button;
    }
	
	/**
	 * 
	 * @param the_paper to use
	 * @return the button responsible for loading the submit recommendation
	 * view for the_paper
	 */
    private JButton createRecommendationButton(final ConferencePaper the_paper)
    {
		JButton button = new JButton("Submit Recommendation");
		button.setPreferredSize(row_item_width);
		if(!the_paper.getRecommendation().equals(ConferencePaper.NOTSUBMITTED)){
			button.setText("Re-Submit");
			button.setBackground(Color.GREEN);
		}
		button.addActionListener(new AbstractAction()
	      {  

			@Override
	        public void actionPerformed(final ActionEvent the_event)
	        {
	        	SPCViewport.loadRecommendationPanel(the_paper);
	        }
	      });		
		return button;
    	
    }

}
